/*
 * Copyright (c) 2017.
 * @author fff
 * 17-10-24 上午10:52
 *
 */
$(function(){
    var parentHeight,parentWidth;
    parentHeight = $("#cooperator-box").height() - 73;
    parentWidth = $("#cooperator-box").width();
    $("#cooperator-table").jqGrid({
        url: contextPath + "/Cooperator/queryCooperatorJqg",
        datatype: "json",
        height: parentHeight,
        width: parentWidth,
        colNames:['','','单位名称','合作模式','信用度','合作级别','责任人','联系电话','认证情况',  '相关资质','人员储备情况', '备注', '操作'],
        colModel:[
            {name:"id",hidden:true},
            {name:'iconFile',hidden:true},
            {name:'name',index:'name',align:"center"},
            {name:'mode',index:'mode',align:"center"},
            {name:'credit',index:'credit',align:"center"},
            {name:'copLevel',index:'copLevel',align:"center"},
            {name:'leader',index:'leader',align:"center"},
            {name:'telephone',index:'telephone',align:"center"},
            {name:'authentication',index:'authentication',align:"center"},
            {name:'certificate',index:'certificate',align:"center"},
            {name:'resource',index:'resource',align:"center"},
            {name:'note',index:'note',align:"center"},

            {name:'moduleId',index:'moduleId',align:"center",formatter: operation}

        ],
        sortname:'id',
        sortorder:'asc',
        viewrecords: true,
        rowNum: 20,
        rowList:[5,10,20],
        pager: '#cooperator-pager'
    });
    $("#cooperator-table").jqGrid('navGrid', '#cooperator-pager', {edit : false,add : false,del : false,search: false});

    // 查询按钮
    $("#query").on("click",function(){
        $("#cooperator-table").setGridParam({
            url : contextPath + "/Cooperator/queryCooperatorJqg",
            postData : {
                queryCond:{
                    'pattern': $('#cooperatorName').val()
                }
            },
            page: 1
        }).trigger("reloadGrid");
    });
});
function autodivheight(){
    parentHeight = $("#cooperator-box").height() - 73;
    parentWidth = $("#cooperator-box").width();
}
function operation(cellvalue,options,rowObject){
    var str = "";
    // if([[${#authorization.expression('hasAuthority(''modifyAuthority'')')}]]){
    str += "<input class='btn btn-primary btn-xs modify' type='button' value='修改'>";
    // }
    // if([[${#authorization.expression('hasAuthority(''deleteAuthority'')')}]]){
    str += "<input class='btn btn-danger btn-xs del' type='button' value='删除' data-toggle='modal' data-target='#deleteNotice'>";
    // }
    return str;
}
//修改
var cooperatorData = null;
/* 修改 || 删除 */
$("#cooperator-table").on('click','.modify,.del',function(){
    var id = $(this).parent().parent().attr('id');
    cooperatorData = $("#cooperator-table").jqGrid('getRowData',id);
    delete cooperatorData.operate;
    if($(this).hasClass("modify")){
        window.location.hash = "qualitycontrol-cooperatorGroup-modifyCooperator";
    }else{
//    	cooperatorData.id = Number(cooperatorData.id);
    }
    console.log(cooperatorData);
});
$("#deleteData").on('click',function(){
    $.ajax({
        url:contextPath + '/Cooperator/deleteCooperator',
        type:"delete",
        contentType:"application/json",
        dataType:"json",
        data: JSON.stringify(cooperatorData),
        success:function(data){
            if(data.code == 1){
                $('#deleteNotice').modal('hide');
                toastr.success('删除成功！');
                // $("#cooperator-table").trigger("reloadGrid");
                $("#cooperator-table").setGridParam({
                    url : contextPath + "/Cooperator/queryCooperatorJqg",
                    postData : {
                        queryCond:{
                            'pattern': $('#cooperatorName').val()
                        }
                    },
                    page: 1
                }).trigger("reloadGrid");

                cooperatorData = null;
            }else{
                toastr.error(data.message,'删除失败！');
            }
        },
        error:function(data){
            console.log(data);
            toastr.error('操作失败，请稍后再试！');
        }
    });
});
$(window).resize(function(){
    $("#cooperator-table").setGridWidth($('#cooperator-box').width());
    $("#cooperator-table").setGridHeight($('#cooperator-box').height() - 73);
});　

